package com.myblog.mapper;

import com.myblog.model.Role;
import com.myblog.model.User;
import javafx.scene.chart.ValueAxis;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;

import java.util.List;

@Mapper
public interface UserMapper {

    List<Integer> searchUserRole(@Param(value = "userId") Integer userId);

    @Insert("insert into user (phone,username,password,gender,avatarImgUrl) value(#{phone},#{username},#{password},#{gender},#{avatarImgUrl});")
    public void addUser(User user);

    @Select("select * from user where username = #{username}")
    User selectUserByName(String name);

    @Select("select * from user where phone = #{phone}")
    User selectUserByPhone(String phone);

    @Select("select * from user where id = #{id}")
    User selectUserById(Integer id);

    @Select("select username from user where id = #{id}")
    String selectUserNameById(Integer id);

    void updateRecentlyLanded(@Param(value = "username") String username, @Param(value = "recentlyLanded")String recentlyLanded);

    List<Role> queryRolesByPhone(String phone);

    void updateRolesByUserId(@Param(value = "userId") Integer userId, @Param(value = "roleId")Integer roleId);

    @Select("select count(*) from user")
    Integer selectNumOfUser();

    @Select("select id from user where phone = #{phone}")
    Integer selectUserIdByPhone(String userPhone);

    @Select("select avatarImgUrl from user where id = #{id}")
    String selectAvatarImgUrlById(@Param(value = "id") Integer answererId);

    @Select("select id from user where username = #{username}")
    Integer selectUserIdByUsername(@Param(value = "username") String respondentName);

    @Select("select phone from user where id = #{id}")
    String selectUserPhoneById(@Param(value = "id")int answerId);

    @Update("update user set password = #{password} where id = #{id} ")
    void updatePassword(@Param(value = "id") Integer userId,@Param(value = "password") String newPassword);

    @Update("update user set password = #{password},phone = #{phone} where id = #{id} ")
    void updatePhoneAndPassword(@Param(value = "id")Integer userId, @Param(value = "phone") String phone, @Param(value = "password")String newPassword);
    
    void updateUserByPhone(User newUser);

    void updateUserByPhoneNoUserName(User newUser);

    @Update("update user set avatarImgUrl=#{avatarImgUrl} where phone = #{phone}")
    void updateAvatarImgByPhone(@Param(value = "phone") String phone,@Param(value = "avatarImgUrl")String imgUrl);

    @Update("update user set phone = #{newPhone} where id = #{id} ")
    void updatePhoneById(Integer id, String newPhone);
}
